package org.jiamandan.com.strategy.impl.tram.listener;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.constant.JSYConstant;
import org.jiamandan.com.entity.RechargePile;
import org.jiamandan.com.strategy.impl.tram.IJSYDZBasicListener;
import org.jiamandan.com.utils.JsyObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Jmd

 * @see:
 * @创建日期：2023-05-08 13:51
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-05-08 13:51	         Jmd        	创建
 * @end
 */
@Service(value = JSYConstant.Service.CMD_JSY_T00001_SERVICE)
@AllArgsConstructor
@Slf4j
public class JSYDZHeartbeatListener implements IJSYDZBasicListener {


    @Override
    public void doExecute(String payload, ChannelHandlerContext client) throws Exception {
        log.info("接收心跳包(CMD = T00001)：" + payload);
        String[] items = StringUtils.split(payload, BizConstant.separator);
        String version = items[3];
        String pileNo = items[5];
        StringBuilder builder = new StringBuilder();
        builder.append("_" + JSYConstant.Cmd.S00001).append("/0025:01/");
        builder.append(JsyObjectUtil.currentDateTime()).append("\r\n");
        log.info("===T00001="+client.channel().id()+"===="+pileNo+"=========");
        log.info("JMD-JSY设备号：【" + pileNo + "】心跳包应答(CMD = S00001):" + builder.toString());
        ByteBuf buf = Unpooled.buffer(builder.toString().getBytes().length);
        buf.writeBytes(builder.toString().getBytes());
        ChannelFuture future = client.writeAndFlush(buf);
    }

}
